﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    public class MapNav : ControlBase
    {
        protected override void Render(HtmlTextWriter writer)
        {
            IList<MapNode> list = GetMapNodes(NodeNames);
            if (list.Count <= 0)
                return;

            if (!string.IsNullOrEmpty(Css))
                writer.AddAttribute(HtmlTextWriterAttribute.Class, Css);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            foreach (MapNode node in list)
            {
                if(node.Visible)
                    RenderItem(writer, node);
            }
            writer.RenderEndTag();
        }

        private void RenderItem(HtmlTextWriter writer, MapNode node)
        {
            if (IsCurrentNode(node))
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "selected");
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.AddAttribute(HtmlTextWriterAttribute.Href,node.Url);
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            if (NavType == MapNavType.Image)
            {
                string image = IsCurrentNode(node) ? node.ImageAction : node.Image;

                writer.AddAttribute(HtmlTextWriterAttribute.Alt, node.Text);
                writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                writer.AddAttribute("image",Page.GetThemeElement(image));
                writer.AddAttribute("imageAction", Page.GetThemeElement(node.ImageAction));
                writer.AddAttribute(HtmlTextWriterAttribute.Src,Page.GetThemeElement(image));
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();

                Page.ClientScript.RegisterStartupScript(typeof(MapNav), this.ClientID, "$(\"#" + this.ClientID + "\").ImageChange();", true);
            }
            else if (NavType == MapNavType.Text)
            {
                writer.Write(node.Text);
            }
            else
            {
            }
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        private bool IsCurrentNode(MapNode node)
        {
            if(SiteContext.Current.MapNode==null)
                return false;

            return (SiteContext.Current.MapNode.Path.IndexOf(node.Path) == 0 && node.Path != "home")
                || (SiteContext.Current.MapNode.Path == "home" && SiteContext.Current.MapNode.Path == node.Path);   
        }

        private IList<MapNode> GetMapNodes(string nodeNames)
        {
            if(string.IsNullOrEmpty(nodeNames))
                return Maps.Content.GetChildNodes(MapType.Gloabls, string.Empty);

            IList<MapNode> nodes = new List<MapNode>();
            string[] names = nodeNames.Split(',');
            foreach (string name in names)
            {
                MapNode node = Maps.Content.GetMapNode(Type, name);
                if (node != null)
                    nodes.Add(node);
            }

            return nodes;
        }

        public string Css { get; set; }

        public MapNavType NavType { get; set; }

        public MapType Type { get; set; }

        /// <summary>
        /// 要显示的nodeName集合，多个之间用","隔开
        /// </summary>
        public string NodeNames { get; set; }
    }

    public enum MapNavType
    {
        Image,
        Text,
        Icon
    }
}
